package com.ruoyi.delivery.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.delivery.domain.DelTransportDetail;
import com.ruoyi.delivery.domain.DelTransportOrder;
import com.ruoyi.delivery.service.IDelTransportOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/delivery/transportOrder")
public class DelTransportOrderController {

    @Autowired
    private IDelTransportOrderService transportOrderService;

    /**
     * 获取运输订单列表，支持分页和筛选
     *
     * @param trackingNumber 运单号
     * @param origin         发货地
     * @param destination    目的地
     * @param startDate      出发时间开始范围
     * @param endDate        出发时间结束范围
     * @param state          运单状态
     * @param customerType   客户类型
     * @param page           页码
     * @param pageSize       每页记录数
     * @return 分页运输订单列表
     */
    @GetMapping("/list")
    public Map<String, Object> getTransportOrderList(
            @RequestParam(required = false) String trackingNumber,
            @RequestParam(required = false) String origin,
            @RequestParam(required = false) String destination,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String customerType,
            @RequestParam(required = false) String orderNumber,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ){
        // 获取数据
        String logisticId = String.valueOf(SecurityUtils.getUserId());
        List<DelTransportOrder> orders = transportOrderService.filterTransportOrders(
                logisticId, trackingNumber, origin, destination, startDate, endDate, state, customerType, page, pageSize
        );

        //获取数据总数
        int total = transportOrderService.getTransportOrderCount(
                logisticId, trackingNumber, origin, destination, startDate, endDate, state, customerType
        );

        // 组装分页信息
        Map<String, Object> response = new HashMap<>();
        response.put("data", orders);
        response.put("total", total);
        response.put("page", page);
        response.put("pageSize", pageSize);
        return response;
    }

    /**
     * 添加运输订单
     *
     * @param order 运输订单对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public Map<String, Object> addTransportOrder(@RequestBody DelTransportOrder order) {
        boolean success = transportOrderService.insertTransportOrder(order);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 删除运输订单
     *
     * @param trackingNumber 运单号
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Map<String, Object> deleteTransportOrder(@RequestParam String trackingNumber) {
        boolean success = transportOrderService.deleteTransportOrder(trackingNumber);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 修改运输订单
     *
     * @param order 运输订单对象
     * @param approach 修改方式
     * @return 操作结果
     */
    @PutMapping("/update")
    public Map<String, Object> updateTransportOrder(@RequestBody DelTransportOrder order, @RequestParam String approach) {
        if(approach.equals("edit")) {
            order.setState("待运营商确认");
        }
        boolean success = transportOrderService.updateTransportOrder(order);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 获取运输订单明细
     *
     * @param trackingNumber 运单号
     * @return 运输订单明细列表
     */
    @GetMapping("/details")
    public Map<String, Object> getTransportOrderDetails(@RequestParam String trackingNumber) {
        List<DelTransportDetail> details = transportOrderService.getDetailsByTrackingNumber(trackingNumber);
        Map<String, Object> response = new HashMap<>();
        response.put("data", details);
        return response;
    }

    /**
     * 确认运输订单
     *
     * @param trackingNumber 运单号
     * @return 操作结果
     */
    @PutMapping("/confirm")
    public Map<String, Object> confirmTransportOrder(@RequestParam String trackingNumber) {
        Integer success = transportOrderService.confirmTransportOrder(trackingNumber,"1009898");
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 生成配送单
     *
     * @param dto 生成配送单请求
     */
    @PostMapping("/generateDeliveryOrder")
    public Map<String, Object> generateDeliveryOrder(@RequestBody GenerateDTO dto) {
        var result = transportOrderService.generateDeliveryOrder(dto.logisticDeliver, dto.exclusionVehicle, dto.trackingNumberList);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("result", result);
        return response;
    }

    public static class GenerateDTO {
        public String logisticDeliver;
        public List<String> exclusionVehicle;
        public List<String> trackingNumberList;
    }

    /**
     * 修改预期到货时间
     * @param trackingNumber 运输单号
     * @param departureDate 新的到货时间
     * @return 操作结果
     */
    @PutMapping("/updateArrivalTime")
    public String updateArrivalTime(@RequestParam String trackingNumber,
                                    @RequestParam String departureDate) {
        try {
            transportOrderService.changeExpectedArrivalTime(trackingNumber, departureDate.replace("T", " ").replace("Z", ""), "1");
            return "预期到货时间更新成功";
        } catch (Exception e) {
            return "预期到货时间更新失败: " + e.getMessage();
        }
    }

}
